import { Controller, Post, Get, Body, Param } from '@nestjs/common';
import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { Comment } from './entities/comment.entity';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Public } from 'src/auth/auth.decorator';
import { Response } from '../../infra/schema/response';

@ApiTags('comments')
@Controller('comments')
export class CommentController {
  constructor(private readonly commentService: CommentService) {}

  @Public()
  @ApiOperation({ summary: '创建评论' })
  @ApiResponse({ status: 201, description: '评论创建成功', type: Comment })
  @Post()
  create(@Body() createCommentDto: CreateCommentDto): Promise<Comment> {
    return this.commentService.create(createCommentDto);
  }

  @Public()
  @ApiOperation({ summary: '获取话题的所有评论' })
  @ApiResponse({
    status: 200,
    description: '成功获取话题的所有评论',
    type: [Comment],
  })
  @Get(':topicId')
  async findByTopicId(@Param('topicId') topicId: string) {
    const result = await this.commentService.findByTopicId(+topicId);
    return Response.successList(result.data, result.total);
  }

  // 新增通过用户 ID 获取评论的接口
  @Public()
  @ApiOperation({ summary: '通过用户 ID 获取评论' })
  @ApiResponse({
    status: 200,
    description: '成功通过用户 ID 获取评论',
    type: [Comment],
  })
  @Get('myComment/:userId')
  async findByUserId(@Param('userId') userId: string) {
    const result = await this.commentService.findByUserId(+userId);
    return Response.successList(result.data, result.total);
  }
}
